{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import joblib\n",
    "import os\n",
    "from joblib import Parallel, delayed"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "concat_01 = joblib.load('./concats_more/concat_01.lz4')\n",
    "concat_02 = joblib.load('./concats_more/concat_02.lz4')\n",
    "concat_03 = joblib.load('./concats_more/concat_03.lz4')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "# z有等于0的情况\n",
    "my_dict = {'x':'Vibration_1', \n",
    "             # 'z':'Vibration_2',\n",
    "             'y':'Vibration_3',\n",
    "             'distance_xyz' : 'Vibration'}\n",
    "\n",
    "def gen_cross(df, my_dict):\n",
    "    '''\n",
    "    描述：\n",
    "        按照my_dict对应关系提取\n",
    "    '''\n",
    "    \n",
    "    def basis_func(key, val, whole_df):\n",
    "        use_cols = [col for col in whole_df.columns if val in col]\n",
    "        df_slice = whole_df[use_cols]\n",
    "        new_cols = [col + ' / %s'%key for col in df_slice.columns]\n",
    "        data = df_slice.values/whole_df[key].values.reshape(-1,1)  # 广播\n",
    "        return pd.DataFrame(data=data, columns=new_cols)\n",
    "    \n",
    "    df_list = Parallel(n_jobs=48, verbose=10)(delayed(basis_func)(key, val, df.copy()) for key, val in my_dict.items())\n",
    "    concat_list = [df] + df_list\n",
    "    tmp = pd.concat(concat_list, axis=1)\n",
    "    print('null:',tmp.isnull().sum(axis=1).sum())\n",
    "    tmp.dropna(inplace=True)\n",
    "    tmp.reset_index(drop=True, inplace=True)\n",
    "    \n",
    "    finite_cond = np.sum(np.isfinite(tmp) == False, axis=1) == 0\n",
    "    tmp_1 = tmp.loc[finite_cond]\n",
    "    tmp_1.reset_index(drop=True, inplace=True)\n",
    "    return tmp_1\n",
    "    return tmp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[Parallel(n_jobs=48)]: Using backend LokyBackend with 48 concurrent workers.\n",
      "[Parallel(n_jobs=48)]: Done   1 tasks      | elapsed:   13.2s\n",
      "[Parallel(n_jobs=48)]: Done   3 out of   3 | elapsed:   25.4s remaining:    0.0s\n",
      "[Parallel(n_jobs=48)]: Done   3 out of   3 | elapsed:   25.4s finished\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "null: 0\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[Parallel(n_jobs=48)]: Using backend LokyBackend with 48 concurrent workers.\n",
      "[Parallel(n_jobs=48)]: Done   3 out of   3 | elapsed:    8.5s remaining:    0.0s\n",
      "[Parallel(n_jobs=48)]: Done   3 out of   3 | elapsed:    8.5s finished\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "null: 0\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[Parallel(n_jobs=48)]: Using backend LokyBackend with 48 concurrent workers.\n",
      "[Parallel(n_jobs=48)]: Done   1 tasks      | elapsed:    3.2s\n",
      "[Parallel(n_jobs=48)]: Done   3 out of   3 | elapsed:    9.8s remaining:    0.0s\n",
      "[Parallel(n_jobs=48)]: Done   3 out of   3 | elapsed:    9.8s finished\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "null: 0\n"
     ]
    }
   ],
   "source": [
    "concat_01_cross = gen_cross(concat_01, my_dict)\n",
    "concat_02_cross = gen_cross(concat_02, my_dict)\n",
    "concat_03_cross = gen_cross(concat_03, my_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['./concats_cross/concat_03.lz4']"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "if not os.path.exists('./concats_cross'):\n",
    "    os.mkdir('./concats_cross')\n",
    "joblib.dump(concat_01_cross, './concats_cross/concat_01.lz4', compress='lz4')\n",
    "joblib.dump(concat_02_cross, './concats_cross/concat_02.lz4', compress='lz4')\n",
    "joblib.dump(concat_03_cross, './concats_cross/concat_03.lz4', compress='lz4')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
